<?php

namespace app\site\controller;

use app\common\model\ShopGoodsGiftModel;
use app\common\model\ShopGoodsSkuModel;
use think\Request;

class ShopGoodsGiftController extends BaseController
{
    private $goodsSkuModel;
    private $giftModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->giftModel = new ShopGoodsGiftModel();
        $this->goodsSkuModel = new ShopGoodsSkuModel();
    }

    /**
     * 赠品组合列表
     */
    public function index()
    {
        $query          = $this->request->request();
        $map['site_id'] = $this->getSiteId();
        $list           = $this->giftModel->field('sku_id,goods_name,sku_name,goods_thumb')->distinct(true)->where($map)->paginate(12, false, ['query' => $query]);
        $page           = $list->render();
        $list           = $list->getCollection()->toArray();
        foreach ($list as $k => $v) {
            $list[$k]['item'] = $this->giftModel->where('site_id', $this->getSiteId())->where('sku_id', $v['sku_id'])->select()->toArray();
        }
        $this->assign('page', $page);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加赠品组合
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'sku_id|主商品'  => 'require',
                'sku_list|赠品' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $dataList = [];
            foreach ($data['sku_list'] as $v) {
                if ((int)$v['gift_num'] > 0) {
                    $v['site_id']     = $data['site_id'];
                    $v['site_name']   = $data['site_name'];
                    $v['goods_id']    = $data['goods_id'];
                    $v['sku_id']      = $data['sku_id'];
                    $v['goods_name']  = $data['goods_name'];
                    $v['sku_name']    = $data['sku_name'];
                    $v['goods_thumb'] = $data['goods_thumb'];
                    $dataList[]       = $v;
                }
            }
            if ($dataList) {
                cache('goods_gift'.$data['sku_id'],null); //先清除缓存
                $this->giftModel->saveAll($dataList);
                $this->goodsSkuModel->where('sku_id',$data['sku_id'])->setField('have_gift',1); //设置商品状态
                $this->success('操作成功！', url('index'));
            } else {
                $this->error('参数不完整！');
            }
        }
        return $this->fetch();
    }

    /**
     * 删除赠品活动
     */
    public function del()
    {
        $skuId = $this->request->param('sku_id/d');
        cache('goods_gift'.$skuId,null); //先清除缓存
        $this->goodsSkuModel->where('sku_id',$skuId)->setField('have_gift',0); //设置商品状态
        $this->giftModel->where('sku_id', $skuId)->delete();
        $this->success('操作成功！');
    }
    /**
     * 增加指定赠品
     */
    public function addGoods()
    {
        $skuId       = $this->request->param('sku_id/d');
        $giftNum     = $this->request->param('gift_num/d');
        $giftSkuInfo = $_POST['gift_sku_info'];
        $giftSkuInfo = json_decode($giftSkuInfo, true);
        if (empty($giftSkuInfo)) {
            $this->error('请选择赠品!');
        }
        if ($this->giftModel->where('sku_id', $skuId)->where('gift_sku_id', $giftSkuInfo['sku_id'])->find()) {
            $this->error('该商品已作为赠品存在！');
        }
        if($giftNum<=0){
            $this->error('赠品数量必须大于0！');
        }
        $data                     = $this->giftModel->field('site_id,site_name,goods_id,sku_id,goods_name,sku_name,goods_thumb')->where('sku_id', $skuId)->find()->toArray();
        $data['gift_goods_id']    = $giftSkuInfo['goods_id'];
        $data['gift_sku_id']      = $giftSkuInfo['sku_id'];
        $data['gift_goods_name']  = $giftSkuInfo['goods_name'];
        $data['gift_sku_name']    = $giftSkuInfo['sku_name'];
        $data['gift_goods_thumb'] = $giftSkuInfo['goods_thumb'];
        $data['gift_num']         = $giftNum;
        cache('goods_gift'.$skuId,null); //先清除缓存
        $this->giftModel->addData($data);
        $this->success('操作成功！');
    }

    /**
     * 删除指定赠品
     */
    public function delGoods()
    {
        $giftId = $this->request->param('gift_id/d');
        $skuId = $this->giftModel->where('gift_id', $giftId)->value('sku_id');
        $count = $this->giftModel->where('sku_id',$skuId)->count();
        if($count<=1){
            $this->error('赠品全部删除请直接删除组合！');
        }
        cache('goods_gift'.$skuId,null); //先清除缓存
        $this->giftModel->where('gift_id', $giftId)->delete();
        $this->success('操作成功！');
    }
}